<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('poiData:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="大类">
                <a-select
                  style="width: 200px"
                  placeholder="请选择大类"
                  allowClear
                  @change="loadsubTagList"
                >
                  <a-select-option v-for="(item) in tagList" :key="item.name" :value="item.id" >{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="中类">
                <a-select
                  style="width: 200px"
                  placeholder="请选择中类"
                  allowClear
                  @change="changeSubTag"
                >
                  <a-select-option v-for="(item) in subTagList" :key="item.name" :value="item.name" >{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="门店名称">
                  <a-input style="width: 200px" v-model="queryParam.name" allow-clear placeholder="请输入门店名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="省会">
                  <a-select
                    v-model="queryParam.proCode"
                    style="width: 200px"
                    placeholder="请选择省份"
                    allowClear
                    @change="loadCity(),queryParam.citCode='',queryParam.regCode=''"
                  >
                    <a-select-option v-for="(item) in ProData" :key="item.areaCode" :value="item.areaCode" >{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="城市">
                  <a-select
                    v-model="queryParam.citCode"
                    style="width: 200px"
                    allowClear
                    placeholder="请选择城市"
                    @change="loadReg(),queryParam.regCode=''"
                  >
                    <a-select-option v-for="(item) in CityData" :key="item.areaCode" :value="item.areaCode" >{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>

              <a-col :md="8" :sm="24">
                <a-form-item label="区/县">
                  <a-select
                    v-model="queryParam.regCode"
                    style="width: 200px"
                    placeholder="请选择区/县"
                    allowClear
                  >
                    <a-select-option v-for="(item) in RegData" :key="item.areaCode" :value="item.areaCode" >{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="年份">
                  <a-input style="width: 200px" v-model="queryParam.year" allow-clear placeholder="请输入年份"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('poiData:add')" >
          <a-button type="primary" v-if="hasPerm('poiData:add')" icon="plus" @click="$refs.addForm.add()">导入POI数据</a-button>
          <!--          <a-button type="danger" :disabled="selectedRowKeys.length < 1" v-if="hasPerm('poiData:delete')" @click="batchDelete"><a-icon type="delete"/>批量删除</a-button>-->
          <x-down
            v-if="hasPerm('poiData:export')"
            ref="batchExport"
            title="最多导出5000条记录"
            @batchExport="batchExport"
          />
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('poiData:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('poiData:edit') & hasPerm('poiData:delete')"/>
          <a-popconfirm v-if="hasPerm('poiData:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
        <span slot="location" slot-scope="text, record">
          <a :href="getMapUrl(record)" target="_blank">查看</a>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
  import { STable, XDown } from '@/components'
  import moment from 'moment'
  import { poiDataPage, poiDataDelete, poiDataExport } from '@/api/modular/main/poidata/poiDataManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import { listCity, listProvince, listRegion } from '@/api/modular/main/biindexdata/biIndexDataManage'
  import { poiDataTagsList } from '@/api/modular/main/poidatatags/poiDataTagsManage'
  import _ from 'lodash'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      XDown
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: { proCode: '',
          citCode: '',
          regCode: '' },
        // 省data
        ProData: {},
        // 市data
        CityData: {},
        // 区data
        RegData: {},
        tagList: [],
        subTagList: [],
        // 表头
        columns: [
          {
            title: '年份',
            align: 'center',
            dataIndex: 'year'
          },
          {
            title: '门店名称',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '省会',
            align: 'center',
            dataIndex: 'provName'
          },
          {
            title: '城市',
            align: 'center',
            dataIndex: 'cityName'
          },
          {
            title: '地区名称',
            align: 'center',
            dataIndex: 'countryName'
          },
          {
            title: '门店地址',
            align: 'center',
            dataIndex: 'address'
          },
          // {
          //   title: '门店电话',
          //   align: 'center',
          //   dataIndex: 'telephone'
          // },
          {
            title: '大类',
            align: 'center',
            dataIndex: 'tag'
          },
          {
            title: '中类',
            align: 'center',
            dataIndex: 'subTag'
          },
          {
            title: '位置',
            align: 'center',
            scopedSlots: { customRender: 'location' }
          }

        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return poiDataPage(Object.assign(parameter, this.switchingDate())).then((res) => {
            console.info(parameter)
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
      }
    },
    created () {
      // if (this.hasPerm('poiData:edit') || this.hasPerm('poiData:delete')) {
      //   this.columns.push({
      //     title: '操作',
      //     width: '150px',
      //     dataIndex: 'action',
      //     scopedSlots: { customRender: 'action' }
      //   })
      // }
      this.loadProvince()
      this.loadTagList()
    },
    methods: {
      moment,
      getMapUrl(record) {
        return 'https://uri.amap.com/marker?position=' + record.lng + ',' + record.lat
      },
      /**
       * 查询参数组装
       */
      switchingDate () {
        const queryParamcreateDate = this.queryParam.createDateDate
        if (queryParamcreateDate != null) {
            this.queryParam.createDate = moment(queryParamcreateDate).format('YYYY-MM-DD')
            if (queryParamcreateDate.length < 1) {
                delete this.queryParam.createDate
            }
        }
        const queryParammodifyDate = this.queryParam.modifyDateDate
        if (queryParammodifyDate != null) {
            this.queryParam.modifyDate = moment(queryParammodifyDate).format('YYYY-MM-DD')
            if (queryParammodifyDate.length < 1) {
                delete this.queryParam.modifyDate
            }
        }
        const obj = JSON.parse(JSON.stringify(this.queryParam))
        return obj
      },
      /**
       * 单个删除
       */
      singleDelete (record) {
        const param = [{ 'id': record.id }]
        this.poiDataDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        this.poiDataDelete(paramIds)
      },
      poiDataDelete (record) {
        poiDataDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      onChangecreateDate(date, dateString) {
        this.createDateDateString = dateString
      },
      onChangemodifyDate(date, dateString) {
        this.modifyDateDateString = dateString
      },
      /**
       * 批量导出
       */
      batchExport () {
        poiDataExport(this.queryParam).then((res) => {
            this.$refs.batchExport.downloadfile(res)
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      loadTagList() {
        const _that = this
        poiDataTagsList({ parentId: 0 }).then((res) => {
          if (res.success) {
            _that.tagList = res.data
          }
        })
      },
      loadsubTagList(parentId) {
        const _that = this
        _that.queryParam.tag = _.find(_that.tagList, (item) => { return item.id === parentId }).name
        _that.queryParam.subTag = ''

        poiDataTagsList({ parentId: parentId }).then((res) => {
          if (res.success) {
            _that.subTagList = res.data
          }
        })
      },
      changeSubTag(value) {
        this.queryParam.subTag = value
      },
      /**
       * 省份查询
       */
      loadProvince() {
        const _that = this
        listProvince().then((res) => {
          if (res.success) {
            _that.ProData = res.data
          }
        })
      },
      /**
       * 市 查询
       */
      loadCity() {
        const _that = this
        listCity(this.queryParam).then((res) => {
          if (res.success) {
            _that.CityData = res.data
          }
        })
      },
      /**
       * 区/县 查询
       */
      loadReg() {
        const _that = this
        listRegion(this.queryParam).then((res) => {
          if (res.success) {
            _that.RegData = res.data
          }
        })
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
